package cn.lcvc.xnc.application.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.lcvc.xnc.application.common.exception.model.MyWebException;
import cn.lcvc.xnc.application.common.utils.file.MyFileUpload;
import cn.lcvc.xnc.application.dao.WasteDisposalSiteMapper;
import cn.lcvc.xnc.application.dao.WasteDisposalSiteVirescenceMapper;
import cn.lcvc.xnc.application.dao.WasteDisposalSiteVirescencePictureMapper;
import cn.lcvc.xnc.application.model.WasteDisposalSite;
import cn.lcvc.xnc.application.model.WasteDisposalSiteVirescence;
import cn.lcvc.xnc.application.model.WasteDisposalSiteVirescencePicture;
import cn.lcvc.xnc.application.model.query.WasteDisposalSiteVirescenceQuery;
import cn.lcvc.xnc.application.service.WasteDisposalSiteService;
import cn.lcvc.xnc.application.service.WasteDisposalSiteVirescenceService;
import cn.lcvc.xnc.authority.model.SysUser;
import cn.lcvc.xnc.authority.service.impl.IBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

/**
 * 复绿Service
 *
 * @author 凡渊
 * @date 2022/08/08 09:27:28
 */
@Service
public class WasteDisposalSiteVirescenceServiceImpl extends IBaseService<WasteDisposalSiteVirescence, WasteDisposalSiteVirescenceQuery> implements WasteDisposalSiteVirescenceService {

    @Autowired
    WasteDisposalSiteMapper wasteDisposalSiteMapper;
    @Autowired
    WasteDisposalSiteVirescenceMapper wasteDisposalSiteVirescenceMapper;
    @Autowired
    WasteDisposalSiteVirescencePictureMapper wasteDisposalSiteVirescencePictureMapper;


    @Override
    public List<WasteDisposalSiteVirescence> findAll(WasteDisposalSiteVirescenceQuery query) {
        List<WasteDisposalSiteVirescence> all = super.findAll(query);
        for (WasteDisposalSiteVirescence wasteDisposalSiteVirescence : all) {
            WasteDisposalSite wasteDisposalSite = wasteDisposalSiteMapper.findById(wasteDisposalSiteVirescence.getSiteId());
            wasteDisposalSiteVirescence.setWasteDisposalSite(wasteDisposalSite);
        }
        return all;
    }

    @Override
    protected void beforeGet(Serializable id) {

    }

    @Override
    protected void beforeUpdate(WasteDisposalSiteVirescence wasteDisposalSiteVirescence) {

    }

    @Override
    protected void beforeSave(WasteDisposalSiteVirescence wasteDisposalSiteVirescence) {

    }

    @Override
    protected void beforeDelete(Serializable[] ids) {

    }

    public void uploadHeadImgFile(Integer id, MultipartFile[] file, String remark) {
        for (MultipartFile files : file) {
            if (files.isEmpty()) {
                throw new MyWebException("请选择要上传的图片");
            } else {
                //图片保存位置
                String filePath = "C:\\img\\";
                //创建文件mulu
                FileUtil.mkdir(filePath);
                //获取文件名
                String fileName = files.getOriginalFilename();
                //获取文件扩展名
                String extensionName = FileUtil.extName(fileName);
//                修改文件名为时间戳
                fileName = String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()) + "." + extensionName;
                //获取文件类型
                String fileType = files.getContentType();
                if (fileType.equals("image/jpeg") || fileType.equals("image/png") || fileType.equals("image/jpg")) {
                    if (files.getSize() / 1000 > 1000) {
                        throw new MyWebException("图片大小不能超过1M,请重新选择要上传的图片");
                    }
                    try {
                        WasteDisposalSiteVirescencePicture pic = new WasteDisposalSiteVirescencePicture();
                        pic.setVirescenceId(id);
                        pic.setPictureUrl(fileName);
                        pic.setRemark(remark);
                        wasteDisposalSiteVirescencePictureMapper.save(pic);
                        files.transferTo(new File(filePath + fileName));
                    } catch (IOException e) {
                        throw new MyWebException("上传失败：" + e.getMessage());
                    }
                } else {
                    throw new MyWebException("图片格式错误");
                }
            }
        }
    }
}
